define(function (require, exports, module) {
    require('authorize');
    require('permission');
    require('global');
    
    var $ = require("jquery");
    var angular = require('angular');
    var util = require('util'); 
    require("pagination");
    require('angular-interceptor');
    require("angular-ui");
    require("angular-messages");
    var ui = require("ui");

    require('service/my_application/appstoreService');


    angular.module('appStore.controllers', [])
        .controller('appStoreController', [
            '$rootScope',
            '$scope', 
            'appStoreService',
            function ($rootScope,$scope, service) {             
                $scope.type = "package";
                
                $scope.buyPackageUrl = "buy.shtml?package=";
                
                $scope.getAppList = function(){
                    service.getAppList({
                        search: {},
                        success: function(res) {
                            $scope.myAppList = res.data;
                        },
                        fail: function(res) {
                            
                        }
                    })
                };
                
                $scope.getAppList();              
                
                $scope.packageList = service.getPackageList();              
                
                $scope.initTrial = function($dialogScope,pkg){
                    $dialogScope.applyPackage = function () {

                        if (!$dialogScope.userName) {
                            $rootScope.$emit('httpRequestError', "请填写姓名");
                            return false;
                        }
                        if (!/^1\d{10}$/.test($dialogScope.phone)) {
                            $rootScope.$emit('httpRequestError', "请填写正确的手机号");
                            return false;
                        }
                        service.applyPackage({
                            data: {
                                userName: $dialogScope.userName,
                                phone: $dialogScope.phone,
                                applyName: pkg.name
                            },
                            success: function (res) {
                                ui.showSucTip();
                            },
                            fail: function (res) {
                                //ui.showErrTip(res.errMsg);
                            }
                        })
                    }
                };
                
                $scope.changeType = function(type) {
                    $scope.type = type;
                };

                $scope.beforeShowUseProject = function($dialog,$dialogScope,appCode){
                    $dialogScope.projects = [];
                    $dialogScope.query = {
                        keyword : ''
                    };
                    $dialogScope.fuzzySearch = function(){
                        service.getUseProjects({
                            appCode : appCode,
                            projName : $dialogScope.query.keyword
                        }).then(function(res){
                            $dialogScope.projects = res.data.data;
                        });
                    };
                    $dialogScope.fuzzySearch();
                };
                
            }
        ]);

    angular.module('appStore', [
        'ngUI',
        'ngInterceptor',
        'appStore.services', 
        'appStore.controllers'
    ]);

    angular.element(document).ready(function(){
        angular.bootstrap(document, ['appStore']);
    })
    
});